<?php

namespace app\modules\admin\controllers;

use Yii;
use app\modules\admin\models\FormFields;
use app\modules\admin\models\FormFieldsSearch;
use app\modules\admin\components\BaseAdminController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\components\Wechat;

/**
 * FormFieldsController implements the CRUD actions for FormFields model.
 */
class FormFieldsController extends BaseAdminController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all FormFields models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new FormFieldsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single FormFields model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new FormFields model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        //微信 教育背景
        $model = new FormFields();
        if (Yii::$app->request->isPost){
           $request = Yii::$app->request->post();
           $datas = $request['FormFields'];
            $optional_form = array_keys($datas,'optional_form');
            $required_form = array_keys($datas,'required_form');
            $data = [
            "card_id" => "p2Rmrt4JId6z4hETx3HTVnLZ68K4",
            "service_statement" => [
            "name" => "会员守则",
                "url"=> "https://www.qq.com"
            ],
            "required_form" => [
                    "can_modify" => true,
                    'common_field_id_list' =>
                        $required_form
                ],
            "optional_form" => [
                "can_modify" => true,
                'common_field_id_list' =>
                    $optional_form
                ]
            ];
            $re = Wechat::card_activate_form($data);
            if ($re['errmsg'] == "ok"){
                    $optional_form = json_encode($optional_form);
                    $required_form = json_encode($required_form);
                    $model->required_form = $required_form;
                    $model->optional_form =$optional_form;
                    $model->tenant = Yii::$app->session->get('identifier');
                    if ($model->save()) {
                        return $this->redirect(['view', 'id' => $model->id]);
                    }
                }
            }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing FormFields model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if (Yii::$app->request->isPost){
            $request = Yii::$app->request->post();
            $datas = $request['FormFields'];
            $optional_form = array_keys($datas,'optional_form');
            $required_form = array_keys($datas,'required_form');
            $data = [
                "card_id" => "p2Rmrt4JId6z4hETx3HTVnLZ68K4",
                "service_statement" => [
                    "name" => "会员守则",
                    "url"=> "https://www.qq.com"
                ],
                "required_form" => [
                    "can_modify" => true,
                    'common_field_id_list' =>
                        $required_form
                ],
                "optional_form" => [
                    "can_modify" => true,
                    'common_field_id_list' =>
                        $optional_form
                ]
            ];
            $re = Wechat::card_activate_form($data);
            if ($re['errmsg'] == "ok"){
                $optional_form = json_encode($optional_form);
                $required_form = json_encode($required_form);
                $model->required_form = $required_form;
                $model->optional_form =$optional_form;
                $model->tenant = Yii::$app->session->get('identifier');
                if ($model->save()) {
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            }
        }

        $required_form = json_decode($model->required_form);
        $optional_form = json_decode($model->optional_form);
        if (in_array('USER_FORM_INFO_FLAG_MOBILE',$required_form)){
            $model->USER_FORM_INFO_FLAG_MOBILE = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_MOBILE',$optional_form)){
            $model->USER_FORM_INFO_FLAG_MOBILE = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_SEX',$required_form)){
            $model->USER_FORM_INFO_FLAG_SEX = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_SEX',$optional_form)){
            $model->USER_FORM_INFO_FLAG_SEX = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_NAME',$required_form)){
            $model->USER_FORM_INFO_FLAG_NAME = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_NAME',$optional_form)){
            $model->USER_FORM_INFO_FLAG_NAME = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_BIRTHDAY',$required_form)){
            $model->USER_FORM_INFO_FLAG_BIRTHDAY = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_BIRTHDAY',$optional_form)){
            $model->USER_FORM_INFO_FLAG_BIRTHDAY = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_IDCARD',$required_form)){
            $model->USER_FORM_INFO_FLAG_IDCARD = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_IDCARD',$optional_form)){
            $model->USER_FORM_INFO_FLAG_IDCARD = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_EMAIL',$required_form)){
            $model->USER_FORM_INFO_FLAG_EMAIL = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_EMAIL',$optional_form)){
            $model->USER_FORM_INFO_FLAG_EMAIL = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_LOCATION',$required_form)){
            $model->USER_FORM_INFO_FLAG_LOCATION = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_LOCATION',$optional_form)){
            $model->USER_FORM_INFO_FLAG_LOCATION = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_INDUSTRY',$required_form)){
            $model->USER_FORM_INFO_FLAG_INDUSTRY = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_INDUSTRY',$optional_form)){
            $model->USER_FORM_INFO_FLAG_INDUSTRY = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_INCOME',$required_form)){
            $model->USER_FORM_INFO_FLAG_INCOME = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_INCOME',$optional_form)){
            $model->USER_FORM_INFO_FLAG_INCOME = 'optional_form';
        }
        if (in_array('USER_FORM_INFO_FLAG_HABIT',$required_form)){
            $model->USER_FORM_INFO_FLAG_HABIT = 'required_form';
        }elseif (in_array('USER_FORM_INFO_FLAG_HABIT',$optional_form)){
            $model->USER_FORM_INFO_FLAG_HABIT = 'optional_form';
        }

        return   $this->render('update', [
            'model' => $model,
        ]);

    }

    /**
     * Deletes an existing FormFields model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the FormFields model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return FormFields the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = FormFields::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
